package models

import (
	__ "ew-srv/basic/proto"
	"gorm.io/gorm"
)

type EVideoass struct {
	gorm.Model
	Title    string `gorm:"type:varchar(20);not null;comment:'课程标题'"`
	Image    string `gorm:"type:varchar(290);not null;comment:'课程封面'"`
	Url      string `gorm:"type:varchar(290);not null;comment:'视频播放地址'"`
	TimeLong string `gorm:"type:varchar(290);not null;comment:'视频播放时长'"`
	VideoNum int    `gorm:"type:int;comment:'视频集数'"`
	Status   int    `gorm:"type:int;comment:'视频状态 1 未发布 2 下架 3 上架'"`
}

func (v *EVideoass) GetVideo(db *gorm.DB, id int) error {
	err := db.Model(&EVideoass{}).Where("id = ?", id).Find(&v).Error
	return err
}

func (v *EVideoass) GetVideoList(db *gorm.DB, page, pageSize int) (List []*__.EVideoList, err error) {
	err = db.Model(&Eclass{}).Scopes(Paginate(page, pageSize)).Find(&List).Error
	return
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
